package com.zj.core.adapter

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.zj.core.R
import com.zj.core.ui.ListFormItemUi
import java.util.ArrayList
import org.jetbrains.anko.*

/**
 * Created by Administrator on 2019/5/23.
 */

class ListFormAdapter(context: Context, mDetailList: ArrayList<String>?,formList: List<String>?) :
    RecyclerView.Adapter<ListFormAdapter.RecordOcrAdapterHolder>() {
    private var mContext: Context? = context
    private var mFormList: ArrayList<String>? = mDetailList
    private var mOldFormList:List<String>? = formList

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecordOcrAdapterHolder {
        return RecordOcrAdapterHolder(ListFormItemUi().createView(AnkoContext.create(parent.context,parent)))
    }

    override fun onBindViewHolder(holder: RecordOcrAdapterHolder, position: Int){
        with(holder){
            for(index in 0 until mOldFormList!!.size){
                if(mOldFormList!![index]==mFormList!![position]){
                    this.recordImg.tag=2
                    this.recordValueTv.textColor= mContext?.resources?.getColor(R.color.colorsRed)!!
                    this.recordImg.setImageDrawable(mContext?.getDrawable(R.drawable.circular_tag_textview))
                }
            }
            this.recordConstraint.setOnClickListener{
                if(this.recordImg.tag==2){
                    this.recordImg.tag=1
                    this.recordValueTv.textColor= Color.BLACK
                    this.recordImg.setImageDrawable(mContext?.getDrawable(R.drawable.circular_tag2_textview))
                }else{
                    this.recordImg.tag=2
                    this.recordValueTv.textColor= mContext?.resources?.getColor(R.color.colorsRed)!!
                    this.recordImg.setImageDrawable(mContext?.getDrawable(R.drawable.circular_tag_textview))
                }
            }
            this.recordValueTv.text = mFormList!![position]
        }
    }

    override fun getItemCount(): Int {
        return mFormList!!.size
    }

    inner class RecordOcrAdapterHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val recordConstraint:ConstraintLayout=itemView.find(R.id.record_item_parent_view)
        val recordImg:ImageView=itemView.find(R.id.record_item_form_img)
        val recordValueTv:TextView=itemView.find(R.id.record_item_form_value)
    }
}